package cn.jlonely.xyhx.app.controller;


import cn.jlonely.xyhx.app.service.UseService;
import cn.jlonely.xyhx.pojo.User;
import cn.jlonely.xyhx.pojo.vo.AppUserVo;
import cn.jlonely.xyhx.pojo.vo.UserVo;
import cn.jlonely.xyhx.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
@RequestMapping("/app")
public class UseController {
    @Autowired
    private UseService useService;

    /**
     * 用户登录
     * @param userVo
     * @return
     * @throws Exception
     */
    @PostMapping("/login")
    public Result login(@RequestBody UserVo userVo) throws Exception {
        User user = useService.login(userVo);
        // 检查 user 是否为 null
        if (user == null) {
            return Result.error("用户名或密码错误");
        }

        if (user.getUserStatus()==1){
            return Result.error("用户已被禁用");
        }

        if (user.getUserStatus()==0){
            return Result.success(user);
        }
        return Result.error("未知错误，请联系管理员");
    }

    @PostMapping("/register")
    public Result register(@RequestBody AppUserVo appUserVo) {
        try {
            useService.register(appUserVo);
            return Result.success();
        } catch (Exception e) {
            return Result.error("用户名已经存在");
        }
    }

    /**
     * 更新用户信息
     * @param appUserVo
     * @return
     */

    @PostMapping("/update")
    public Result update(@RequestBody AppUserVo appUserVo) {
            useService.update(appUserVo);
            return Result.success();
    }


}
